package com.crm.manager.service.impl;

import com.crm.manager.domain.Activity;
import com.crm.manager.domain.Contracts;
import com.crm.manager.domain.SnowflakeIdGenerator;
import com.crm.manager.domain.dto.ContractDto;
import com.crm.manager.domain.vo.PageVO;
import com.crm.manager.mapper.ContractMapper;
import com.crm.manager.service.ContractService;
import com.crm.manager.service.CourseService;
import com.crm.manager.service.IActivityService;
import com.crm.manager.utils.BaseContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
@Slf4j
public class ContractServiceImpl implements ContractService {


    private final ContractMapper contractMapper;
    private final CourseService courseService;
    private final IActivityService activityService;



    // 创建雪花算法实例
    private static final
    SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1);
    @Override
    public int addContract(ContractDto contract) {


            // 使用雪花算法生成 ID
            long snowflakeId = idGenerator.nextId();
            contract.setId(String.valueOf(snowflakeId)); // 将 long 类型的 ID 转换为字符串



        // 设置创建时间和更新时间为当前时间
        contract.setCreateTime(LocalDateTime.now());
        contract.setUpdateTime(LocalDateTime.now());
        contract.setCreateBy(BaseContext.getUserName());


        // 根据课程 ID 查询课程价格
        if (contract.getCourseId() != null) {
            BigDecimal coursePrice = courseService.getCoursePriceById(contract.getCourseId());
            contract.setCoursePrice(coursePrice); // 设置课程价格
        }

        // 调用 Mapper 插入数据
        return contractMapper.insertContract(contract);
    }

    @Override
    public Contracts getContractById(String id) {
        // 查询合同信息
        Contracts contract = contractMapper.getContractById(id);

        if (contract != null) {
            // 将 id 转换回 String 类型
            contract.setId(id);

            // 根据课程ID查询课程价格
            if (contract.getCourseId() != null) {
                BigDecimal coursePrice = courseService.getCoursePriceById(contract.getCourseId());
                contract.setCoursePrice(coursePrice);
            }

            // 根据活动ID查询折扣类型
            if (contract.getActivityId() != null) {
                String discountType = activityService.getDiscountTypeById(contract.getActivityId());
                // 将折扣类型从数字代码转换为文字描述
                String discountDescription = mapDiscountType(discountType);
                contract.setDiscountType(discountDescription);
            }
        }

        return contract;
}
    private String mapDiscountType(String code) {
        switch (code) {
            case "1":
                return "课程折扣";
            case "2":
                return "课程代金卷";
            default:
                return "未知折扣";
        }
    }
    @Override
    public Contracts ContractById(Integer id) {
        return contractMapper.ContractById(id);
    }

    @Override
    public void update(Contracts contracts) {
        contractMapper.updata(contracts);
    }

    @Override
    public PageVO page(ContractDto contractDto) {
        PageHelper.startPage(contractDto.getPageNum(), contractDto.getPageSize());
        List<Activity> list = contractMapper.page(contractDto);
        Page<Activity> page = (Page<Activity>) list;
        long total = page.getTotal();
        PageVO pageVO = new PageVO();
        pageVO.setTotal(total);
        pageVO.setRows(page.getResult());
        return pageVO;

    }


}